R Markdown

This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see http://rmarkdown.rstudio.com.

When you click the Knit button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:

Graph_Attributes<-data.table::fread(here::here("data", "CGCS-GraphData-NodeTypes.csv"))
s1 <- data.table::fread(here::here("dataQ2","dfSeed1Reduced.csv"))
s3 <- data.table::fread(here::here("dataQ2","dfSeed3Reduced.csv"))
s1_network <- subset(s1, select = c(Source, Target, Weight))
s3_network <- subset(s3, select = c(Source, Target, Weight))
s1_edgelist <- s1_network
s1_graph <- graph.data.frame(s1_edgelist, directed = TRUE)

s3_edgelist <- s3_network
s3_graph <- graph.data.frame(s3_edgelist, directed = TRUE)

s1_graph2<-igraph::simplify(s1_graph, remove.multiple = FALSE, remove.loops = TRUE)
s3_graph2<-igraph::simplify(s3_graph, remove.multiple = FALSE, remove.loops = TRUE)

tail(s1, 10)
##       V1 Source eType Target     Time Weight SourceLocation
##  1: 2551 481408     6 561157 17712000      4              3
##  2:  834 626174     6 561157 23155200      1              3
##  3: 2675 462691     6 561157 24019200      6              3
##  4: 2548 481408     6 509607 11836800      4              3
##  5:  147 552243     6 509607 20995200      0              3
##  6: 2596 594696     6 509607 31363200      5              3
##  7: 3011 564784     2 547205 22653139      4             NA
##  8: 5053 583737     3 547205 22653139      4             NA
##  9: 3010 564784     2 547205 19623898     80             NA
## 10: 4940 583737     3 547205 19623898     80             NA
unique(Graph_Attributes$NodeType)
## [1] 1 4 3 2 5
s1_edgelist <- s1_edgelist[!duplicated(s1_edgelist)]

S1:

## + 83/83 vertices, named, from 38d1848:
##  [1] 600971 564804 626174 470784 554368 638591 477333 533691 558444 490882
## [11] 594696 462691 631159 605385 506539 564784 466429 532462 583737 631490
## [21] 510031 552988 575811 547483 474123 481408 620120 493846 469603 542731
## [31] 573191 537659 527214 603212 521273 597450 591024 643930 479674 495487
## [41] 573678 589043 584511 552243 501914 536173 630700 529018 571938 579269
## [51] 536346 520660 567195 527449 459381 595298 466907 589943 577992 537281
## [61] 523927 580426 642329 503701 644226 632961 473173 630626 616315 575030
## [71] 621924 503218 571970 640784 595581 606730 499467 616453 625756 657173
## [81] 561157 509607 547205
## [1] 793

Vis Network:

Sources <- s1 %>%
  distinct(Source) %>%
  rename(label = Source)

Targets <- s1 %>%
  distinct(Target) %>%
  rename(label = Target)

# Creating a Node List:
nodes <- full_join(Sources, Targets, by = "label")
nodes
##     label
## 1  600971
## 2  564804
## 3  626174
## 4  470784
## 5  554368
## 6  638591
## 7  477333
## 8  533691
## 9  558444
## 10 490882
## 11 594696
## 12 462691
## 13 631159
## 14 605385
## 15 506539
## 16 564784
## 17 466429
## 18 532462
## 19 583737
## 20 631490
## 21 510031
## 22 552988
## 23 575811
## 24 547483
## 25 474123
## 26 481408
## 27 620120
## 28 493846
## 29 469603
## 30 542731
## 31 573191
## 32 537659
## 33 527214
## 34 603212
## 35 521273
## 36 597450
## 37 591024
## 38 643930
## 39 479674
## 40 495487
## 41 573678
## 42 589043
## 43 584511
## 44 552243
## 45 501914
## 46 536173
## 47 630700
## 48 529018
## 49 571938
## 50 579269
## 51 536346
## 52 520660
## 53 567195
## 54 527449
## 55 459381
## 56 595298
## 57 466907
## 58 589943
## 59 577992
## 60 537281
## 61 523927
## 62 580426
## 63 642329
## 64 503701
## 65 644226
## 66 632961
## 67 473173
## 68 630626
## 69 616315
## 70 575030
## 71 621924
## 72 503218
## 73 571970
## 74 640784
## 75 595581
## 76 606730
## 77 499467
## 78 616453
## 79 625756
## 80 657173
## 81 561157
## 82 509607
## 83 547205
nodes <- nodes %>% rowid_to_column("id")
nodes
##    id  label
## 1   1 600971
## 2   2 564804
## 3   3 626174
## 4   4 470784
## 5   5 554368
## 6   6 638591
## 7   7 477333
## 8   8 533691
## 9   9 558444
## 10 10 490882
## 11 11 594696
## 12 12 462691
## 13 13 631159
## 14 14 605385
## 15 15 506539
## 16 16 564784
## 17 17 466429
## 18 18 532462
## 19 19 583737
## 20 20 631490
## 21 21 510031
## 22 22 552988
## 23 23 575811
## 24 24 547483
## 25 25 474123
## 26 26 481408
## 27 27 620120
## 28 28 493846
## 29 29 469603
## 30 30 542731
## 31 31 573191
## 32 32 537659
## 33 33 527214
## 34 34 603212
## 35 35 521273
## 36 36 597450
## 37 37 591024
## 38 38 643930
## 39 39 479674
## 40 40 495487
## 41 41 573678
## 42 42 589043
## 43 43 584511
## 44 44 552243
## 45 45 501914
## 46 46 536173
## 47 47 630700
## 48 48 529018
## 49 49 571938
## 50 50 579269
## 51 51 536346
## 52 52 520660
## 53 53 567195
## 54 54 527449
## 55 55 459381
## 56 56 595298
## 57 57 466907
## 58 58 589943
## 59 59 577992
## 60 60 537281
## 61 61 523927
## 62 62 580426
## 63 63 642329
## 64 64 503701
## 65 65 644226
## 66 66 632961
## 67 67 473173
## 68 68 630626
## 69 69 616315
## 70 70 575030
## 71 71 621924
## 72 72 503218
## 73 73 571970
## 74 74 640784
## 75 75 595581
## 76 76 606730
## 77 77 499467
## 78 78 616453
## 79 79 625756
## 80 80 657173
## 81 81 561157
## 82 82 509607
## 83 83 547205
# Creating an Edge List:
per_route <- s1 %>%  
  group_by(Source, Target) %>%
  summarise(weight = n()) %>% 
  ungroup()
per_route
## # A tibble: 788 x 3
##    Source Target weight
##     <int>  <int>  <int>
##  1 462691 459381      2
##  2 462691 466429      1
##  3 462691 466907      2
##  4 462691 470784      1
##  5 462691 473173      2
##  6 462691 503701      2
##  7 462691 523927      2
##  8 462691 527449      2
##  9 462691 536346      2
## 10 462691 537281      2
## # ... with 778 more rows
edges <- per_route %>% 
  left_join(nodes, by = c("Source" = "label")) %>% 
  rename(from = id)

edges <- edges %>% 
  left_join(nodes, by = c("Target" = "label")) %>% 
  rename(to = id)

edges <- select(edges, from, to, weight)
edges
## # A tibble: 788 x 3
##     from    to weight
##    <int> <int>  <int>
##  1    12    55      2
##  2    12    17      1
##  3    12    57      2
##  4    12     4      1
##  5    12    67      2
##  6    12    64      2
##  7    12    61      2
##  8    12    54      2
##  9    12    51      2
## 10    12    60      2
## # ... with 778 more rows
# Creating a Network:
routes_network <- network(edges, vertex.attr = nodes, matrix.type = "edgelist", ignore.eval = FALSE)
class(routes_network)
## [1] "network"
routes_network
##  Network attributes:
##   vertices = 83 
##   directed = TRUE 
##   hyper = FALSE 
##   loops = FALSE 
##   multiple = FALSE 
##   bipartite = FALSE 
##   total edges= 788 
##     missing edges= 0 
##     non-missing edges= 788 
## 
##  Vertex attribute names: 
##     id label vertex.names 
## 
##  Edge attribute names: 
##     weight
plot(routes_network, vertex.cex = 3, main = "Seed-1 Graph")